<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-form-item label="用户编号" prop="userId">
        <el-input v-model="dataForm.userId" placeholder="用户编号"></el-input>
      </el-form-item>
      <el-form-item label="用户登录名" prop="userLoginname">
        <el-input v-model="dataForm.userLoginname" placeholder="用户登录名"></el-input>
      </el-form-item>
      <el-form-item label="用户登录密码" prop="userPassword">
        <el-input v-model="dataForm.userPassword" type="password" placeholder="用户登录密码"></el-input>
      </el-form-item>
      <el-form-item label="户主姓名" prop="userUsername">
        <el-input v-model="dataForm.userUsername" placeholder="户主姓名"></el-input>
      </el-form-item>
      <el-form-item label="用户地址" prop="userAddress">
        <el-input v-model="dataForm.userAddress" placeholder="用户地址"></el-input>
      </el-form-item>
      <el-form-item label="用户手机号" prop="userPhone">
        <el-input v-model="dataForm.userPhone" placeholder="用户手机号"></el-input>
      </el-form-item>
      <el-form-item label="水表编号" prop="waterId">
        <el-input v-model="dataForm.waterId" placeholder="用户水表编号"></el-input>
      </el-form-item>
      <el-form-item label="电表编号" prop="powerId">
        <el-input v-model="dataForm.powerId" placeholder="用户电表编号"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          userLoginname: '',
          userPassword: '',
          userUsername: '',
          userAddress: '',
          userPhone: '',
          waterId: '',
          powerId: ''
        },
        dataRule: {
          userId: [
            {required: true, message: '用户编号不能为空', trigger: 'blur'}
          ],
          userLoginname: [
            {required: true, message: '用户登录名不能为空', trigger: 'blur'}
          ],
          userPassword: [
            {required: true, message: '用户登录密码不能为空', trigger: 'blur'}
          ],
          userUsername: [
            {required: true, message: '户主姓名不能为空', trigger: 'blur'}
          ],
          userAddress: [
            {required: true, message: '用户地址不能为空', trigger: 'blur'}
          ],
          userPhone: [
            {required: true, message: '用户手机号不能为空', trigger: 'blur'}
          ],
          waterId: [
            {required: true, message: '水表编号不能为空', trigger: 'blur'}
          ],
          powerId: [
            {required: true, message: '电表编号不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/use/appuser/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.appUser.userId
                this.dataForm.userLoginname = data.appUser.userLoginname
                this.dataForm.userPassword = data.appUser.userPassword
                this.dataForm.userUsername = data.appUser.userUsername
                this.dataForm.userAddress = data.appUser.userAddress
                this.dataForm.userPhone = data.appUser.userPhone
                this.dataForm.waterId = data.appUser.waterId
                this.dataForm.powerId = data.appUser.powerId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/use/appuser/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'userLoginname': this.dataForm.userLoginname,
                'userPassword': this.dataForm.userPassword,
                'userUsername': this.dataForm.userUsername,
                'userAddress': this.dataForm.userAddress,
                'userPhone': this.dataForm.userPhone,
                'waterId': this.dataForm.waterId,
                'powerId': this.dataForm.powerId
              //  111
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
